﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Lagrange : MonoBehaviour
{
	public static Lagrange Instance;

	public DrawLine drawLine;
	public float interval;
	private float n;
	private float length;
	private List<Vector2> p;
	private List<Vector2> pointList;

	private void Awake() {
		Instance = this;
	}

	// Start is called before the first frame update
	void Start()
    {
		n = RandomPointList.Instance.n;
		length = n * RandomPointList.Instance.interval;
		pointList = new List<Vector2>();
	}

    // Update is called once per frame
    void Update()
    {
        
    }

	public void LagrangeAlgorithm() {
		p = RandomPointList.Instance.randomPointList;
		pointList.Clear();
		for (int i = 0; i * interval < length; i++) {
			float x = i * interval;
			float s = 0;
			for (int k = 0; k <= n; k++) {
				float p = this.p[k].y;
				for (int j = 0; j <= n; j++) {
					if (j!=k) {
						p = p * (x - this.p[j].x) / (this.p[k].x - this.p[j].x);
					}
				}
				s += p;
			}
			//System.Threading.Thread.Sleep(10);
			pointList.Add(new Vector2(x, s));
			//drawLine.UpdateLine();
		}
		drawLine.pointList = pointList;
	}
}
